#! /usr/bin/env python
# -*- coding: utf-8 -*-

# *************************************************************
#     Filename @  app.py
#       Author @  Huoty
#  Create date @  2016-07-28 19:26:34
#  Description @
# *************************************************************

import json

from flask import Flask, request, Response

app = Flask(__name__)


class BadRequest(Exception):
    pass


@app.route('/')
def main():
    return "Yes!"


@app.route('/test_raise')
def test_raise():
    raise BadRequest("异常测试")
    return "hello world"


@app.route('/test_response')
def test_response():
    class JsonResponse(Response):
        """Json 数据相应"""
        def __init__(self, data=None, msg="successful"):
            super(JsonResponse, self).__init__()
            self.mimetype = "application/json"
            self.data = json.dumps({
                "code": 0 if msg == "successful" else 1,
                "msg": msg,
                "data": data
            })

    return JsonResponse(request.args["data"], request.args["msg"])


@app.route('/test_request_values', methods=['GET', 'POST'])
def test_request_values():
    print("request values type:", type(request.values))
    print("request values:", request.values)
    print('a:', request.values.get('a'), 'b:', request.values.get('b'))
    return json.dumps(request.values)


# Script starts from here

if __name__ == "__main__":
    app.run(debug=False)

